package menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controller.Controller;

@SuppressWarnings("serial")
public class Chat extends JFrame implements ActionListener {

	private JPanel contentPane;
	private Controller controller;
	private JTextField chatMsg;
	private volatile JTextArea chatDisplay;

	/**
	 * Create the frame.
	 */
	public Chat(Controller cont) {
		setResizable(false);
		this.controller = cont;
		if(controller.conn.toString().equals("Client"))
			setTitle("Chat Window: Client");
		else
			setTitle("Chat Window: Host");

		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // Make the chat unclosable.
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FormLayout(
				new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						RowSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		chatDisplay = new JTextArea();
		chatDisplay.setLineWrap(true);
		chatDisplay.setEditable(false);
		chatDisplay.setText("Entering map selection...");
		contentPane.add(chatDisplay, "1, 2, 6, 1, fill, fill");

		chatMsg = new JTextField();
		contentPane.add(chatMsg, "1, 4, 4, 1, fill, default");
		chatMsg.setColumns(10);
		chatMsg.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// Hitting enter sends this data
		if (!chatMsg.getText().equals("")) {
			String inputMsg = chatMsg.getText();
			String sendMsg = "CHAT:";
			sendMsg += inputMsg;
			controller.conn.send(sendMsg);
			
			String displayString = chatDisplay.getText() + "\nYou: "+ inputMsg;
			chatDisplay.setText(fitStringToWindow(displayString));
			
			chatMsg.setText("");
		}
	}
	
	private String fitStringToWindow(String inputString){
		String[] splitString = inputString.split("\n");
		String returnString = "" + splitString[1];
		if(splitString.length >= 15){
			for(int i = 2; i < splitString.length; i++){
				returnString += ("\n" + splitString[i]);
			}
		}
		else
			return inputString;
		return returnString;
	}
	
	public void updateText(String newText){
		String displayString = chatDisplay.getText()+ "\nOpponent: " + newText;
		chatDisplay.setText(fitStringToWindow(displayString));
	}
}